package edu.uic.ids517;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

public class DataMiningTool {

	//Creating Object of bean
	ArrayList<DataBean> dataBean;
	ActionBeanDbaseAccess dbaseBean;
	MessageBean messageBean;
	TableListBean tableListBean;
	private List<StatisticalAnalysisBean> statsBean ;

	public DataMiningTool() 
	{
		//displaySelectedColumns();
	}


	public List<StatisticalAnalysisBean> getStatsBean() {
		return statsBean;
	}

	public void setStatsBean(List<StatisticalAnalysisBean> statsBean) {
		this.statsBean = statsBean;
	}

	public ArrayList<DataBean> getDataBean() {
		return dataBean;
	}

	public void setDataBean(ArrayList<DataBean> dataBean) {
		this.dataBean = dataBean;
	}

	public ActionBeanDbaseAccess getDbaseBean() {
		return dbaseBean;
	}

	public void setDbaseBean(ActionBeanDbaseAccess dbaseBean) {
		this.dbaseBean = dbaseBean;
	}

	public MessageBean getMessageBean() {
		return messageBean;
	}

	public void setMessageBean(MessageBean messageBean) {
		this.messageBean = messageBean;
	}

	public TableListBean getTableListBean() {
		return tableListBean;
	}

	public void setTableListBean(TableListBean tableListBean) {
		this.tableListBean = tableListBean;
	}


	public void displaySelectedColumns(){
		System.out.println("1111111111111111111111");
		List<String>  selectedColumnName ;

		selectedColumnName = tableListBean.getColumnName();

		for(int i=0; i<selectedColumnName.size();i++)
		{
			System.out.println("INSISE GET all Columsn.........."+selectedColumnName.get(i));
			String type = selectedColumnName.get(i).getClass().getName();
			System.out.println("INSISE GET all Type.........."+type);
		}

		System.out.println("22222222222");

	}


	public String computeStatisticalAnalysis(){
		String status= "FAILURE";
		System.out.println("INSIDE computeStatisticalAnalysis.................");
		List<Double> selectedColumsList = new ArrayList<Double>();
		int totalColumns = tableListBean.getColumnName().size();
		statsBean = new ArrayList<StatisticalAnalysisBean>();
		try{
			for( int i = 0; i < totalColumns; i++) {
				String columnName1 = tableListBean.getColumnName().get(i);
				selectedColumsList = tableListBean.getAllRowsForSelectedColumn(columnName1);
				System.out.println("1111111..........."+selectedColumsList.size());
				DescriptiveStatistics stats = new DescriptiveStatistics();
				StatisticalAnalysisBean statsBeanTemp = null;
				if(!selectedColumsList.equals(null))
				{
					System.out.println("2222222222...........");
					// Add the data from the array
					for(int j =0; j<selectedColumsList.size();j++)
					{
						System.out.println("333333333..........."+selectedColumsList.get(j));
						stats.addValue(selectedColumsList.get(j));
					}

					System.out.println("4444444444...........");
					// Compute some statistics
					DecimalFormat df = new DecimalFormat("#.##");
					
					
					statsBeanTemp = new StatisticalAnalysisBean();
					statsBeanTemp.setVariableName(columnName1);
					statsBeanTemp.setMean(stats.getMean());	
					
				//	statsBeanTemp.setStandardDeviation(Double.valueOf(df.format(stats.getStandardDeviation())));\
					statsBeanTemp.setStandardDeviation(stats.getStandardDeviation());
					statsBeanTemp.setMedian(stats.getPercentile(50));
					statsBeanTemp.setMinimum(stats.getMin());
					statsBeanTemp.setMaximum(stats.getMax());
					statsBeanTemp.setFirstQuartile(stats.getPercentile(25));
					statsBeanTemp.setSecondQuartile(stats.getPercentile(75));
					
					System.out.println("4444444444....."+stats.getMean());
					System.out.println("4444444444....."+stats.getStandardDeviation());
					System.out.println("4444444444....."+stats.getPercentile(50));
					System.out.println("4444444444....."+stats.getMin());
					System.out.println("4444444444....."+stats.getMax());
					System.out.println("4444444444....."+stats.getPercentile(25));
					System.out.println("4444444444....."+stats.getPercentile(75));
				}
				statsBean.add(statsBeanTemp);
			} 
		}catch (Exception e) {
			e.printStackTrace();
		}

		if(statsBean != null) 
			status = "SUCCESS";



		return status;
	}


}


